package thread;

import lombok.Data;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *
 * volatile 关键字
 * @author Duanyu
 * @date 2021-11-10 11:50
 */
public class VolatileTest {


    @Test
    public void test() {

        ThreadTest test = new ThreadTest();
        new Thread(test).start();

        while (true){
            if (test.isFlag()){
                System.out.println("------------");
                break;
            }
        }
    }

}

@Data
class ThreadTest implements Runnable {

    private volatile boolean flag = false;

    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println(flag);
    }
}
